home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Dr. Windows 3
/
dr win3.zip
/
dr win3
/
VISUALBA
/
BLTQ12.ZIP
/
XB_SRC01.BAS
< prev
next >
Wrap
BASIC Source File
|
1993-01-04
|
18KB
|
596 lines
DECLARE FUNCTION DoBackup% (dfHandle%, kfHandle%)
DECLARE FUNCTION DoExpandFile% (kfHandle%)
DECLARE FUNCTION DoReindex% (kfHandle%)
DECLARE FUNCTION DoAdd% (kfHandle%)
DECLARE FUNCTION DoAddAll% (kfHandle%)
DECLARE FUNCTION DoClose% (dfHandle%, kfHandle%)
DECLARE FUNCTION DoCreateOpenDataFile% (dfHandle%)
DECLARE FUNCTION DoCreateOpenKeyFile% (dfHandle%, kfHandle%)
DECLARE FUNCTION DoExit% ()
DECLARE FUNCTION DoFirstThings% (dfHandle%, kfHandle%)
DECLARE FUNCTION DoGetEqual% (kfHandle%, match$)
DECLARE FUNCTION DoMemCheck% ()
DECLARE SUB DoPrint (kfHandle%, k$)
DECLARE FUNCTION DoShowFirst% (kfHandle%)
DECLARE FUNCTION DoShowNext% (kfHandle%)
DECLARE FUNCTION GetKeyInfo% (kfHandle%, kfKeyFlags%, kfKeyLen%)
DECLARE FUNCTION IsShareLoaded% ()
DEFINT A-Z
REM $INCLUDE: 'BULLET.BI'
'XB_SRC01.BAS 31-May-92 chh
'code example of a BULLET program that uses many of the BULLET routines--
'--though not really that well designed--an early ad-hoc design test bed
TYPE ScoreRecTYPE
tag AS STRING * 1 'MUST HAVE DELETE TAG SPACE DEFINED FOR BULLET USE
codename AS STRING * 6
score AS STRING * 4 'true DBF format has NUMERIC in ASCII, not binary form
END TYPE '11
DIM SHARED gScoreRec AS ScoreRecTYPE 'the only global variable
CONST MAXDF = 1 'max data files to be used concurrently (1-250)
CONST MAXKF = 1 'max key files to be used concurrently (1-250)
CONST MAXFD = 2 'max fields to be used concurrently (SUM of all!)
' (this program has only 2 fields total)
'these values mainly for DoMemCheck here
'all variables are local to main and
'are passed if needed elsewhere rather
'than declaring then SHARED (why not)
'because...
DIM SHARED dfHandle AS INTEGER 'DOS file handle to data file
DIM SHARED kfHandle AS INTEGER 'DOS file handle to key file
'note: if you run this program more than once without first deleting the
'two files this creates, then the program will end with a error 201 since
'the key file was created to all unique keys only (easy enough to change)
'--also, the Creating status will indicate error 80 (&H50) "Already exists"
CLS
PRINT "XSRC01.BAS"
PRINT "----------Key: CHARACTER, NLS, DUPLICATES ALLOWED"
stat = DoFirstThings(dfHandle, kfHandle)
PRINT "Using DOS handles:"; dfHandle; kfHandle
IF stat = 0 THEN
INPUT "How may add loops (max=32000 loops, each loop is 14 recs)", a
ts! = TIMER
FOR i = 1 TO a
stat = DoAddAll(dfHandle)
IF stat THEN EXIT FOR
NEXT
te! = TIMER
PRINT "add rec time"; te! - ts!
IF stat = 0 THEN
ts! = TIMER
stat = DoReindex(kfHandle)
te! = TIMER
IF stat = 0 THEN
stat = stat2
PRINT "reindex time"; te! - ts!
match$ = "SHARKY" + CHR$(0) + CHR$(0)
stat = DoGetEqual(kfHandle, match$)
END IF
END IF
END IF
PRINT "status:"; stat;
SELECT CASE stat
CASE 202
PRINT "Normal End Of File"
CASE 201
PRINT "Keyfile created for UNIQUE keys and attempt to insert key that already exists"
PRINT "Either allow duplicate keys (in CreateKXB) or delete key or delete file"
CASE ELSE
PRINT "Look it up"
END SELECT
END
'data filename, number of fields
'(for each field) name, type, length, decimal count
DataFileInfo:
DATA ".\XSRC01.DBF"
DATA 2
DATA "CODENAME","C",6,0
DATA "SCORE","N",4,0
'key filename, key expression, key flags (see DOCs for flags)
KeyFileInfo:
DATA ".\XSRC01.DEX"
DATA "CODENAME"
DATA 2
'sample data for data file
'codename,score
SampleData:
DATA "SHARKY",100
DATA "Sharki",47
DATA "BRande",48
DATA "BRANDI",95
DATA "BWANA",66
DATA "SaysSo",87
DATA "SAYSNO",50
DATA "SEXIMA",69
DATA "BERLIN",55
DATA "MUNICH",44
DATA "FURTH",77
DATA "Goanna",61
DATA "Spock1",67
DATA "SPOCK2",99
DATA "",0
FUNCTION DoAdd (dfHandle)
'add a new entry into the database, locking all bytes in the key and data
'files if SHARE.EXE is loaded preventing other processes from accessing
'the two files while we're making changes to them
DIM AP AS AccessPack
DIM AnyKeyBuffer AS STRING * 64
ShareLoaded = IsShareLoaded
AP.Func = LockXB 'first lock the key file and data file
AP.Handle = dfHandle
AP.RecPtrOff = VARPTR(gScoreRec) 'point to the data record
AP.RecPtrSeg = VARSEG(gScoreRec)
AP.KeyPtrOff = VARPTR(AnyKeyBuffer) 'point to the key buffer
AP.KeyPtrSeg = VARSEG(AnyKeyBuffer)
AP.NextPtrOff = 0 'point to the next key file (none)
AP.NextPtrSeg = 0
LOCATE , 1
statLock = 0
IF ShareLoaded THEN
AP.Handle = kfHandle 'want the kfHandle for the xaction lock
PRINT "Initiating locks";
stat = BULLET(AP)
IF stat THEN statLock = AP.stat
AP.Handle = dfHandle
END IF
stat = statLock
IF stat = 0 THEN 'and now do the add
'AP.Handle = kfHandle
'AP.Func = InsertXB 'both key and the data record
'!not for this example, using ReindexXB
AP.Func = AddRecordXB 'of just data record
PRINT " - adding rec: "; gScoreRec.codename;
stat = BULLET(AP)
'since for InsertXB (and UpdateXB and LockXB) return not the
'error status but rather the key file position number (since we
'can Insert/Update/Lock up to 32 key files plus a data file at one
'time) we must explicity check for the error status in AP.stat
'(can still check AP.Stat even if not a xaction-based routine!)
stat = AP.stat
IF stat = 0 THEN PRINT " recno:"; AP.RecNo;
END IF
IF ShareLoaded AND (statLock = 0) THEN
AP.Func = UnlockXB 'if lock was successful must unlock
AP.Handle = kfHandle
PRINT " - released locks";
stat = BULLET(AP)
IF stat THEN stat = AP.stat
PRINT stat
END IF
DoAdd = stat
END FUNCTION
FUNCTION DoAddAll (dfHandle)
'read the DATA codename and score and add it to the data file
'and insert its key to the key file
'done for each of the sample data items in SampleData:
'dfHandle is not needed because it is known to BULLET from the Open()
RESTORE SampleData
DO
READ cname$, score$ 'score$ as string because DBF format
IF LEN(cname$) = 0 THEN EXIT DO 'specifies all data in DBF files be
'in ASCII format
gScoreRec.codename = cname$
RSET gScoreRec.score = score$ 'right-justify score in field
stat = DoAdd(dfHandle) 'insert gScoreRec and its key
LOOP UNTIL stat
DoAddAll = stat
END FUNCTION
FUNCTION DoBackup (dfHandle, kfHandle)
'backup the current files
DIM CP AS CopyPack
DIM BUname AS STRING * 64
BUname = ".\XSRC01.D!F" + CHR$(0)
CP.Func = BackupFileXB
CP.Handle = dfHandle
CP.FilenamePtrOff = VARPTR(BUname)
CP.FilenamePtrSeg = VARSEG(BUname)
stat = BULLET(CP)
IF stat = 0 THEN
BUname = ".\XSRC01.D!X" + CHR$(0)
CP.Func = BackupFileXB
CP.Handle = kfHandle
CP.FilenamePtrOff = VARPTR(BUname)
CP.FilenamePtrSeg = VARSEG(BUname)
stat = BULLET(CP)
END IF
DoBackup = stat
END FUNCTION
FUNCTION DoClose (dfHandle, kfHandle)
'close key file first, then data file
DIM HP AS HandlePack
HP.Func = CloseKXB
HP.Handle = kfHandle
stat = BULLET(HP)
HP.Func = CloseDXB
HP.Handle = dfHandle
stat2 = BULLET(HP)
IF stat = 0 THEN stat = stat2
DoClose = stat
END FUNCTION
FUNCTION DoCreateOpenDataFile (dfHandle)
'Create (if needed) and open data file
'Rtn: dfHandle DOS file handle
'--Demonstrates ability to specify data file format at run-time without
'hard-coding it at compile-time. This info could easily be specified
'interactively from the user, an external file, etc.
'FieldName MUST BE ZERO-FILLED TO CHARACTER POSITION 11
'technically, only A-Z and _ are allowed in DBF fieldnames
'also, all info should be in UPPER-CASE
DIM CDP AS CreateDataPack
DIM OP AS OpenPack
DIM XBdf AS STRING * 64 'used only for create (must be FIXED-LENGTH)